﻿using PracticeCentre.Common;
using PracticeCentre.DataControl;
using PracticeCentre.Modeles;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PracticeCentre.Business
{
    public class Positions
    {
        public PageModel<Position> List(PageModel<Position> page)
        {
            DataControlHelper dataHelper = new DataControlHelper();
            try
            {
                SqlParameter[] parameters = new SqlParameter[] {
                    new SqlParameter("@sys_PageIndex",page.PageNumber),
                    new SqlParameter("@sys_PageSize",page.PageSize),
                    new SqlParameter("@sys_Table","Position"),
                    new SqlParameter("@sys_Key","id"),
                    new SqlParameter("@sys_Where",""),
                    new SqlParameter("@sys_Order","id"),
                    new SqlParameter("@sys_AscOrDesc",""),
                    new SqlParameter("@pageCount",page.PageCount),
                    new SqlParameter("@recordCount",page.RowCount)
                };
                parameters[7].Direction = ParameterDirection.Output;
                parameters[8].Direction = ParameterDirection.Output;
                int pagecount = 0;
                int rowcount = 0;
                DataTable dt = dataHelper.ExecuteDataTable<Position>(ProceduresConfig.GeneralPaging,out pagecount,out rowcount, parameters);
                DataModelHandler<Position> dataHandler = new DataModelHandler<Position>();
                List<Position> postList = dataHandler.FillModel(dt);
                page.PageCount = pagecount;
                page.RowCount = rowcount;
                page.DataList = postList;
                return page;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 根据ID获取岗位
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public Position GetModelById(Position model)
        {
            DataControlHelper dataHelper = new DataControlHelper();
            try
            {
                SqlParameter[] parameters = new SqlParameter[] {
                    new SqlParameter("@Id",model.Id)
                };
                DataModelHandler<Position> modeHandler = new DataModelHandler<Position>();
                List<Position> postList= modeHandler.FillModel(dataHelper.ExecuteDataTable(ProceduresConfig.GetPostModel, parameters));
                return postList.Count > 0 ? postList[0] : null;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// 添加新岗位
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Add(Position model)
        {
            DataControlHelper dataHelper = new DataControlHelper();
            try
            {
                SqlParameter[] parameters = new SqlParameter[] {
                    new SqlParameter("@PostName",model.PostName)
                };
                return dataHelper.ExecuteNoQuery(ProceduresConfig.AddNewPosition, parameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// 修改岗位
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Edit(Position model)
        {
            DataControlHelper dataHelper = new DataControlHelper();
            try
            {
                SqlParameter[] parameters = new SqlParameter[] {
                    new SqlParameter("@Id",model.Id),
                    new SqlParameter("@PostName",model.PostName)
                };
                return dataHelper.ExecuteNoQuery(ProceduresConfig.EditPostion, parameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public bool DeleteModel(Position model)
        {
            DataControlHelper dataHelper = new DataControlHelper();
            try
            {
                SqlParameter[] parameters = new SqlParameter[] {
                    new SqlParameter("@Id",model.Id)
                };
                return dataHelper.ExecuteNoQuery(ProceduresConfig.DeletePostModel, parameters);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }


    }
}
